using UniversalAdminSystem.Domian.FileStorage.IRepository;
using UniversalAdminSystem.Domian.FileStorage.ValueObjects;
using UniversalAdminSystem.Infrastructure.Persistence.DbContexts;
using Microsoft.EntityFrameworkCore;
using UniversalAdminSystem.Domian.Core.ValueObjects;
using File = UniversalAdminSystem.Domian.FileStorage.Aggregates.File;
using Microsoft.Extensions.FileProviders;

namespace UniversalAdminSystem.Infrastructure.Persistence.Repositories;

public class FileRepository : BaseRepository<File>, IFileRepository
{
    public FileRepository(UniversalAdminSystemDbContext dbContext) : base(dbContext) { }

    public async Task<IEnumerable<File>> GetByOwnerAsync(UserId ownerId)
        => await _TDs.Where(f => f.OwnerId == ownerId).ToListAsync();

    public async Task<IEnumerable<File>> GetByParentAsync(FileId? parentId)
        => await _TDs.Where(f => f.ParentId == parentId).ToListAsync();

    public async Task<IEnumerable<File>> GetByTypeAsync(FileType type)
        => await _TDs.Where(f => f.Type == type).ToListAsync();

    public async Task<IEnumerable<File>> GetFilesWithAccessAsync(UserId userId, FileAccessLevel accessLevel)
        => await _TDs.Where(f => f.AccessLevel == accessLevel && (f.OwnerId == userId || f.AccessLevel == FileAccessLevel.Public)).ToListAsync();

    public async Task<IEnumerable<File>> GetAllFoldersAsync(UserId ownerId)
        => await _TDs.Where(f => f.OwnerId == ownerId && f.IsFolder).ToListAsync();

    public override async Task<File?> GetByGuidAsync(Guid id)
    {
        var fileId = FileId.Create(id);
        var file =await _TDs.FirstOrDefaultAsync(f => f.Id == fileId);
        return file;
    }
} 